package com.xmh.ssm.utils.reptile;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

@Slf4j
public class SeleniumHello {

    private static WebDriver driver = null;

    static {
        System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(SeleniumHello::runTest).start();
        }
    }

    private static void runTest() {
        String url = "https://www.toutiao.com/a6833281526415753741/";

        long startTime = System.currentTimeMillis();

        getTitle(driver, url);

        System.out.println("耗时2：" + (System.currentTimeMillis() - startTime));

//        new Thread(driver::close).start();
    }

    private synchronized static void getTitle(WebDriver driver, String url) {
        driver.get(url);

        String title = driver.getTitle();

        String src;

        try {
            src = driver.findElement(By.cssSelector("img[alt='" + title + "']")).getAttribute("src");
        } catch (Exception e) {
            log.error("", e);
            return;
        }
        System.out.println("图片地址：" + src);
        System.out.println("标题：" + title);
    }
}

//        System.setProperty("webdriver.firefox.marionette", "C:\\Program Files (x86)\\Google\\Chrome\\Application");
//C:\Program Files (x86)\Google\Chrome\Application
//        WebDriver driver = new FirefoxDriver();
//        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
